/**
  ******************************************************************************
  * File Name          : measure.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention : 注意单位!!!
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "measure.h"
#include "gv.h"
#include "parameter.h"
#include <string.h>
#include <stdlib.h>

void meas_ao420ma_1_out(GlobalVariablesStru *pgv)
{
  uint16_t outvalue;
  int32_t  range;
  float mult, code;
  
  range = pgv->hr.meas_pa.measuring_range_tph * 1000;
  
  if(pgv->ir.meas_ca.flowrate_kgh < range)
  {
    mult = (float)range / (float)pgv->ir.meas_ca.flowrate_kgh;
    code = (float)(pgv->hr.meas_pa.ao1_full_code - pgv->hr.meas_pa.ao1_zero_code) / mult;
    outvalue = code + pgv->hr.meas_pa.ao1_zero_code;
  }
  else
  {
    outvalue = pgv->hr.meas_pa.ao1_full_code;
  }
  
  if(outvalue > 65535)
  {
    outvalue = 65535;
  }
  
  WRITE_REG(TIM3->CCR1, outvalue);
}

//void meas_ao420ma_2_out(GlobalVariablesStru *pgv)
//{
//}

void measure_init(GlobalVariablesStru *pgv)
{
}

/* 安全更改主状态机函数,主状态机只能通过此函数更改*/
void meas_change_msm(Meas_MainStateMachineEnum mbst, GlobalVariablesStru *pgv)
{
  switch(mbst)
  {
    case MEAS_MAIN_STATE_MACHINE_MZERO:
      pgv->ir.meas_ca.calmzero_substmachine = MZERO_SUB_STATE_MACHINE_INIT;
      pgv->ir.meas_ca.main_state_machine = MEAS_MAIN_STATE_MACHINE_MZERO;
      break;

    case MEAS_MAIN_STATE_MACHINE_CALGM:
      pgv->ir.meas_ca.calgm_substmachine = CALGM_SUB_STATE_MACHINE_INIT;
      pgv->ir.meas_ca.main_state_machine = MEAS_MAIN_STATE_MACHINE_CALGM;
      break;

    case MEAS_MAIN_STATE_MACHINE_CALSW:
      pgv->ir.meas_ca.calsw_substmachine = CALSW_SUB_STATE_MACHINE_INIT;
      pgv->ir.meas_ca.main_state_machine = MEAS_MAIN_STATE_MACHINE_CALSW;
      break;

    case MEAS_MAIN_STATE_MACHINE_CALLM:
    case MEAS_MAIN_STATE_MACHINE_IDLE:
    case MEAS_MAIN_STATE_MACHINE_MEASURING:
      pgv->ir.meas_ca.main_state_machine = mbst;
      break;

    default:
      return;
  }
}

void measure_real_time(GlobalVariablesStru *pgv)
{
  int32_t temp;
  double adv;
  double flowrate_src_0d1g_0d01s;   /* 流量计算的原始值,一切计算和累计的根基 */
  double double_flowrate_kgh;
  
  pgv->ir.meas_ca.weight_code_ave = pgv->ir.meas_ad.wad_unified;
  pgv->ir.meas_ca.speed_code_ave = pgv->ir.meas_ad.sad_hz;
  
  /* 流量计算 0.1g/0.01s */
  temp = (pgv->ir.meas_ad.wad_unified - pgv->hr.meas_pa.zero_code_ave) * pgv->ir.meas_ad.sad_hz;
  adv = (double)temp / M_DIV;
  
  flowrate_src_0d1g_0d01s = adv * (double)pgv->hr.meas_pa.weight_factor_base;

  if(pgv->hr.meas_pa.factor_correct_enable == MEAS_FACTOR_CORRECT_ENABLE)
  {
    if(flowrate_src_0d1g_0d01s <= (400.0/F_MUL_0D1G_0D01S_TO_TH))
    {
      flowrate_src_0d1g_0d01s =  adv * (pgv->hr.meas_pa.weight_factor_base + pgv->hr.meas_pa.weight_factor05);
    }
    else if((flowrate_src_0d1g_0d01s > (400.0/F_MUL_0D1G_0D01S_TO_TH)) && (flowrate_src_0d1g_0d01s <= (800.0/F_MUL_0D1G_0D01S_TO_TH)))
    {
      flowrate_src_0d1g_0d01s =  adv * (pgv->hr.meas_pa.weight_factor_base + pgv->hr.meas_pa.weight_factor04);
    }
    else if((flowrate_src_0d1g_0d01s > (800.0/F_MUL_0D1G_0D01S_TO_TH)) && (flowrate_src_0d1g_0d01s <= (1200.0/F_MUL_0D1G_0D01S_TO_TH)))
    {
      flowrate_src_0d1g_0d01s =  adv * (pgv->hr.meas_pa.weight_factor_base + pgv->hr.meas_pa.weight_factor03);
    }
    else if((flowrate_src_0d1g_0d01s > (1200.0/F_MUL_0D1G_0D01S_TO_TH)) && (flowrate_src_0d1g_0d01s <= (1600.0/F_MUL_0D1G_0D01S_TO_TH)))
    {
      flowrate_src_0d1g_0d01s =  adv * (pgv->hr.meas_pa.weight_factor_base + pgv->hr.meas_pa.weight_factor02);
    }
    else if(flowrate_src_0d1g_0d01s > (1600.0/F_MUL_0D1G_0D01S_TO_TH))
    {
      flowrate_src_0d1g_0d01s =  adv * (pgv->hr.meas_pa.weight_factor_base + pgv->hr.meas_pa.weight_factor01);
    }
  }
  
  double_flowrate_kgh = flowrate_src_0d1g_0d01s * F_MUL_0D1G_0D01S_TO_KGH;

  /* 实际流量低于该值时,流量示值将显示为0,且不进行累计。*/
  if((pgv->hr.meas_pa.flowrate_lowlim_kgh != 0) && (double_flowrate_kgh < pgv->hr.meas_pa.flowrate_lowlim_kgh))
  {
    flowrate_src_0d1g_0d01s = 0.0;
    double_flowrate_kgh = 0.0;
  }
  
  /* 在重量和速度实测(非内给)状态下,若通讯出错,流量归零 */
  if((pgv->hr.meas_pa.wad_preset_enable == AD_CODE_ACTUAL) || (pgv->hr.meas_pa.sad_preset_enable == AD_CODE_ACTUAL))
  {
    if(pgv->ir.meas_ca.ssp_state != 0)
    {
      flowrate_src_0d1g_0d01s = 0.0;
      double_flowrate_kgh = 0.0;
    }
  }

  pgv->ir.meas_ca.flowrate_0d1g_0d01s = flowrate_src_0d1g_0d01s;
  pgv->ir.meas_ca.flowrate_kgh = double_flowrate_kgh;

  switch(pgv->ir.meas_ca.main_state_machine)
  {
    case MEAS_MAIN_STATE_MACHINE_MZERO:
      if(meas_manual_zero(pgv) != RET_RUNNING)
      {
        meas_change_msm(MEAS_MAIN_STATE_MACHINE_IDLE, pgv);
      }
      break;

    case MEAS_MAIN_STATE_MACHINE_CALGM:
      if(meas_calibrate_guama(flowrate_src_0d1g_0d01s, pgv) != RET_RUNNING)
      {
        meas_change_msm(MEAS_MAIN_STATE_MACHINE_IDLE, pgv);
      }
      break;

    case MEAS_MAIN_STATE_MACHINE_CALSW:
      if(meas_calibrate_shiwu(flowrate_src_0d1g_0d01s, pgv) != RET_RUNNING)
      {
        meas_change_msm(MEAS_MAIN_STATE_MACHINE_IDLE, pgv);
      }
      break;

    case MEAS_MAIN_STATE_MACHINE_IDLE:
    case MEAS_MAIN_STATE_MACHINE_MEASURING:
    case MEAS_MAIN_STATE_MACHINE_CALLM:
    default:
      meas_accrue(flowrate_src_0d1g_0d01s, pgv);
      meas_auto_zero(pgv);
      break;
  }
}

void measure_cycle(GlobalVariablesStru *pgv)
{
  /* 皮带速度计算 */
  pgv->ir.meas_ca.belt_speed_mms = (float)pgv->ir.meas_ad.sad_hz / (float)pgv->hr.meas_pa.speed_wheel_pls * ((float)pgv->hr.meas_pa.speed_wheel_cir_0d1mm / 10.0);
  /* 皮带单位长度载荷计算 */
  pgv->ir.meas_ca.belt_load_gm = (((float)pgv->ir.meas_ca.flowrate_kgh / (float)pgv->ir.meas_ca.belt_speed_mms)) * (1000.0/3.6);

  meas_ao420ma_1_out(pgv);

  pgv->ir.meas_ca.interval_acc_kg= meas_division_indication(pgv->ir.meas_ca.interval_acc_0d1g,0);
  pgv->ir.meas_ca.total_acc_kg   = meas_division_indication(pgv->ir.meas_ca.total_acc_0d1g,   0);
  pgv->ir.meas_ca.hour_acc_kg    = meas_division_indication(pgv->ir.meas_ca.hour_acc_0d1g,    0);
  pgv->ir.meas_ca.day_acc_kg     = meas_division_indication(pgv->ir.meas_ca.day_acc_0d1g,     0);
  pgv->ir.meas_ca.month_acc_kg   = meas_division_indication(pgv->ir.meas_ca.month_acc_0d1g,   0);
  pgv->ir.meas_ca.year_acc_kg    = meas_division_indication(pgv->ir.meas_ca.year_acc_0d1g,    0);
  pgv->ir.meas_ca.class01_acc_kg = meas_division_indication(pgv->ir.meas_ca.class01_acc_0d1g, 0);
  pgv->ir.meas_ca.class02_acc_kg = meas_division_indication(pgv->ir.meas_ca.class02_acc_0d1g, 0);
  pgv->ir.meas_ca.class03_acc_kg = meas_division_indication(pgv->ir.meas_ca.class03_acc_0d1g, 0);
  pgv->ir.meas_ca.class04_acc_kg = meas_division_indication(pgv->ir.meas_ca.class04_acc_0d1g, 0);
}
